package cn.simon.free.logtrack.core;

import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.UnsynchronizedAppenderBase;
import cn.simon.free.logtrack.utils.LogTrackUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 日志采集核心
 *
 * @author simon
 * @date 2020/12/23 10:37
 * @copyright:
 * @modifier
 * @motto a no talent, people who have to study hard
 */
public class LogAppender<E> extends UnsynchronizedAppenderBase<E> {

    private final Logger log = LoggerFactory.getLogger(LogAppender.class);

    public LogAppender() {
    }

    @Override
    protected void append(E e) {
        appendEvent(e);
    }

    private void appendEvent(E eventObject) {
        //init Event Object
        if (!(eventObject instanceof LoggingEvent)) {
            return;
        }
        LoggingEvent event = (LoggingEvent) eventObject;
        LogTrackUtils.add(event.getFormattedMessage());
    }
}
